package com.android.pa;

import java.util.Calendar;
import java.util.Locale;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class PopUp extends Activity
{

	/* CONSTANTS */

	protected static final int EXTRALARGETIME = 120;
	protected static final int LARGETIME = 60;
	protected static final int MEDIUMTIME = 30;
	protected static final int SMALLTIME = 15;

	private PACalendar _PACalendar;
	private Scheduler _Scheduler;

	TextView tv;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		_PACalendar = new PACalendar(getApplicationContext());
		_Scheduler = new Scheduler(getApplicationContext());
		onNewIntent(getIntent());
	}

	@Override
	public void onNewIntent(Intent intent)
	{
		assert (intent.hasExtra("task"));

		Log.d("Popup", "alarm recieved");
		Log.d("Popup", "Started Popup");
		Log.d("Popup", "set layout");
		Log.d("Popup", "got Intent");

		Task t = null;
		String startEnd = null;

		t = (Task) intent.getSerializableExtra("task");
		assert (t != null);

		Log.d("Popup", "got Task");
		startEnd = intent.getStringExtra("start/end");

		assert (startEnd != null);
		Log.d("Popup", "got startEnd");

		if (startEnd.equals("start"))
			startTask(t);
		else
			endTask(t);
	}

	private void endTask(final Task t)
	{
		Log.d("Popup", "started end task");

		if (t.isFluid())
		{
			setContentView(R.layout.end_task);

			tv = (TextView) findViewById(R.id.end_task_main_text);
			String message = getResources().getString(
					R.string.end_task_main_string_start)
					+ t.getTitle()
					+ getResources().getString(
							R.string.end_task_main_string_end);
			tv.setText(message);
			Log.d("Popup", "started end task message = " + message);

			final Button yesButton = (Button) findViewById(R.id.end_task_yes_button);
			yesButton.setOnClickListener(new View.OnClickListener()
			{
				public void onClick(View v)
				{
					Log.d("Popup", "pressed yes");
					showNextTask(t);
				}
			});

			final Button noButton = (Button) findViewById(R.id.end_task_no_button);
			noButton.setOnClickListener(new View.OnClickListener()
			{
				public void onClick(View v)
				{
					Log.d("Popup", "pressed no");
					moreTime((FluidTask) t);
				}
			});
		} else
		{
			setContentView(R.layout.end_fixed_task);
			tv = (TextView) findViewById(R.id.end_fixed_task_first_line);

			String message = t.getTitle()
					+ " "
					+ getResources().getString(
							R.string.end_fixed_task_main_first_line);
			tv.setText(message);
			Log.d("Popup", "started end task message = " + message);

			Task next_task = _PACalendar.getNextTask(t);

			if (next_task == null)
			{
				tv = (TextView) findViewById(R.id.end_fixed_task_second_line);
				tv.setVisibility(View.GONE);
				tv = (TextView) findViewById(R.id.next_task_name_name);
				tv.setVisibility(View.GONE);
				tv = (TextView) findViewById(R.id.next_task_name_text);
				tv.setVisibility(View.GONE);
				tv = (TextView) findViewById(R.id.next_task_starts_time);
				tv.setVisibility(View.GONE);
				tv = (TextView) findViewById(R.id.next_task_starts_text);
				tv.setVisibility(View.GONE);
				tv = (TextView) findViewById(R.id.next_task_ends_time);
				tv.setVisibility(View.GONE);
				tv = (TextView) findViewById(R.id.next_task_ends_text);
				tv.setVisibility(View.GONE);
				tv = (TextView) findViewById(R.id.next_task_deadline_time);
				tv.setVisibility(View.GONE);
				tv = (TextView) findViewById(R.id.next_task_deadline_text);
				tv.setVisibility(View.GONE);
			} else
			{
				Log.d("Popup", "got next task");
				tv = (TextView) findViewById(R.id.next_task_name_name);
				String title = next_task.getTitle();
				tv.setText(title);
				Log.d("Popup", "start task title = " + title);

				tv = (TextView) findViewById(R.id.next_task_starts_time);
				String starts_time = next_task.getStartTime()
						.get(Calendar.HOUR)
						+ ":"
						+ next_task.getStartTime().get(Calendar.MINUTE);
				tv.setText(starts_time);
				Log.d("Popup", "start task start time = " + starts_time);

				tv = (TextView) findViewById(R.id.next_task_ends_time);
				String ends_time = next_task.getEndTime().get(Calendar.HOUR)
						+ ":" + next_task.getEndTime().get(Calendar.MINUTE);
				tv.setText(ends_time);
				Log.d("Popup", "start task end time = " + ends_time);

				tv = (TextView) findViewById(R.id.next_task_deadline_time);
				Calendar deadline = ((FluidTask) next_task).getDeadline();
				String deadline_time = deadline.getDisplayName(Calendar.MONTH,
						Calendar.LONG, Locale.US)
						+ " "
						+ deadline.get(Calendar.DAY_OF_MONTH)
						+ " at "
						+ deadline.get(Calendar.HOUR)
						+ ":"
						+ deadline.get(Calendar.MINUTE);
				tv.setText(deadline_time);
				Log.d("Popup", "start task deadline time = " + deadline_time);
			}

			final Button hideButton = (Button) findViewById(R.id.next_task_hide);
			hideButton.setOnClickListener(new View.OnClickListener()
			{
				public void onClick(View v)
				{
					Log.d("Popup", "hide");
					finish();
				}
			});
		}
	}

	/**
	 * Add more time to task.
	 * 
	 * @param t
	 *            the task to extend.
	 */
	protected void moreTime(final FluidTask t)
	{
		Log.d("Popup", "in more time");
		setContentView(R.layout.more_time);

		final Button smallTimeButton = (Button) findViewById(R.id.more_time_small);
		smallTimeButton.setOnClickListener(new View.OnClickListener()
		{
			public void onClick(View v)
			{
				
				Calendar cal = Calendar.getInstance();
				cal.clear();
				cal.add(Calendar.MINUTE, SMALLTIME);
				Long timeToAdd = cal.getTimeInMillis();
				String addMessage = _Scheduler.addTimeToTask(t, timeToAdd);
				addTimeToTask(addMessage,t);
			}
		});

		final Button mediumTimeButton = (Button) findViewById(R.id.more_time_large);
		mediumTimeButton.setOnClickListener(new View.OnClickListener()
		{
			public void onClick(View v)
			{
				Calendar cal = Calendar.getInstance();
				cal.clear();
				cal.add(Calendar.MINUTE, MEDIUMTIME);
				Long timeToAdd = cal.getTimeInMillis();
				String addMessage = _Scheduler.addTimeToTask(t, timeToAdd);
				addTimeToTask(addMessage,t);
			}
		});

		final Button largeTimeButton = (Button) findViewById(R.id.more_time_large);
		largeTimeButton.setOnClickListener(new View.OnClickListener()
		{
			public void onClick(View v)
			{
				Calendar cal = Calendar.getInstance();
				cal.clear();
				cal.add(Calendar.MINUTE, LARGETIME);
				Long timeToAdd = cal.getTimeInMillis();
				String addMessage = _Scheduler.addTimeToTask(t, timeToAdd);
				addTimeToTask(addMessage,t);
			}
		});

		final Button moreButton = (Button) findViewById(R.id.more_time_xl);
		moreButton.setOnClickListener(new View.OnClickListener()
		{
			public void onClick(View v)
			{
				Calendar cal = Calendar.getInstance();
				cal.clear();
				cal.add(Calendar.MINUTE, EXTRALARGETIME);
				Long timeToAdd = cal.getTimeInMillis();
				String addMessage = _Scheduler.addTimeToTask(t, timeToAdd);
				addTimeToTask(addMessage,t);
			}
		});
	}

	private void addTimeToTask(String addMessage, Task t){
		if (addMessage != null){
			Log.d("Pop Up", "did not add time");
				Toast.makeText(getApplicationContext(), addMessage,
						Toast.LENGTH_LONG).show();
				}
			else
			{
				Log.d("Pop Up", "added time");
				
				Toast.makeText(getApplicationContext(), "Task rescheduled",
						Toast.LENGTH_LONG).show();
				try
				{
					Thread.sleep(1000);
				} catch (InterruptedException e)
				{
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				showNextTask(t);
			}
	}
	
	
	/**
	 * Shows the next task after finishing a task.
	 * 
	 * @param t
	 *            the task.
	 */
	protected void showNextTask(Task t)
	{
		setContentView(R.layout.next_task);
		Log.d("Popup", "next task screen started");

		Task next_task = _PACalendar.getNextTask(t);

		if (next_task == null)
			finish();

		Log.d("Popup", "got next task");

		tv = (TextView) findViewById(R.id.next_task_name_name);
		String title = next_task.getTitle();
		tv.setText(title);
		Log.d("Popup", "start task title = " + title);

		tv = (TextView) findViewById(R.id.starts_time);
		String starts_time = next_task.getStartTime().get(Calendar.HOUR) + ":"
				+ next_task.getStartTime().get(Calendar.MINUTE);
		tv.setText(starts_time);
		Log.d("Popup", "start task start time = " + starts_time);

		tv = (TextView) findViewById(R.id.ends_time);
		String ends_time = next_task.getEndTime().get(Calendar.HOUR) + ":"
				+ next_task.getEndTime().get(Calendar.MINUTE);
		tv.setText(ends_time);
		Log.d("Popup", "start task end time = " + ends_time);

		tv = (TextView) findViewById(R.id.deadline_time);
		Calendar deadline = ((FluidTask) next_task).getDeadline();
		String deadline_time = deadline.getDisplayName(Calendar.MONTH,
				Calendar.LONG, Locale.US)
				+ " "
				+ deadline.get(Calendar.DAY_OF_MONTH)
				+ " at "
				+ deadline.get(Calendar.HOUR)
				+ ":"
				+ deadline.get(Calendar.MINUTE);
		tv.setText(deadline_time);
		Log.d("Popup", "start task deadline time = " + deadline_time);

		final Button hideButton = (Button) findViewById(R.id.next_task_hide);
		hideButton.setOnClickListener(new View.OnClickListener()
		{
			public void onClick(View v)
			{
				Log.d("Popup", "hide");
				finish();
			}
		});
	}

	/**
	 * Set start task pop up.
	 * 
	 * @param t
	 *            the task.
	 */
	private void startTask(final Task t)
	{
		Log.d("Popup", "started start task");
		if (t.isFluid())
		{
			setContentView(R.layout.start_task);
			Log.d("Popup", "task is fluid task");

			tv = (TextView) findViewById(R.id.task_name_name);
			tv.setText(t.getTitle());
			Log.d("Popup", "start task title = " + t.getTitle());

			tv = (TextView) findViewById(R.id.starts_time);
			String starts_time = t.getStartTime().get(Calendar.HOUR) + ":"
					+ t.getStartTime().get(Calendar.MINUTE);
			tv.setText(starts_time);
			Log.d("Popup", "start task start time = " + starts_time);

			tv = (TextView) findViewById(R.id.ends_time);
			String ends_time = t.getEndTime().get(Calendar.HOUR) + ":"
					+ t.getEndTime().get(Calendar.MINUTE);
			tv.setText(ends_time);
			Log.d("Popup", "start task end time = " + ends_time);

			tv = (TextView) findViewById(R.id.deadline_time);
			Calendar deadline = ((FluidTask) t).getDeadline();
			String deadline_time = deadline.getDisplayName(Calendar.MONTH,
					Calendar.LONG, Locale.US)
					+ " "
					+ deadline.get(Calendar.DAY_OF_MONTH)
					+ " at "
					+ deadline.get(Calendar.HOUR)
					+ ":"
					+ deadline.get(Calendar.MINUTE);
			tv.setText(deadline_time);
			Log.d("Popup", "start task deadline time = " + deadline_time);

			final Button acceptButton = (Button) findViewById(R.id.accept);
			acceptButton.setOnClickListener(new View.OnClickListener()
			{
				public void onClick(View v)
				{
					acceptTask();
				}
			});

			final Button snoozeButton = (Button) findViewById(R.id.snooze);
			snoozeButton.setOnClickListener(new View.OnClickListener()
			{
				public void onClick(View v)
				{
					snoozeTask((FluidTask) t);
				}
			});

			final Button editButton = (Button) findViewById(R.id.edit_task);
			editButton.setOnClickListener(new View.OnClickListener()
			{
				public void onClick(View v)
				{
					editTask(t);
				}
			});
		} else
		{
			setContentView(R.layout.start_fixed_task);
			Log.d("Popup", "task is fixed task");

			tv = (TextView) findViewById(R.id.task_name_name);
			tv.setText(t.getTitle());
			Log.d("Popup", "start task title = " + t.getTitle());

			tv = (TextView) findViewById(R.id.starts_time);
			String starts_time = t.getStartTime().get(Calendar.HOUR) + ":"
					+ t.getStartTime().get(Calendar.MINUTE);
			tv.setText(starts_time);
			Log.d("Popup", "start task start time = " + starts_time);

			tv = (TextView) findViewById(R.id.ends_time);
			String ends_time = t.getEndTime().get(Calendar.HOUR) + ":"
					+ t.getEndTime().get(Calendar.MINUTE);
			tv.setText(ends_time);
			Log.d("Popup", "start task end time = " + ends_time);

			final Button acceptButton = (Button) findViewById(R.id.hide);
			acceptButton.setOnClickListener(new View.OnClickListener()
			{
				public void onClick(View v)
				{
					finish();
				}
			});
		}
	}

	/**
	 * Edit the current task.
	 * 
	 * @param t
	 *            the task.
	 */
	protected void editTask(Task t)
	{
		// TODO Auto-generated method stub
		// call main activity for intent, pass it the task in an intent with the
		// action edit
	}

	/**
	 * Snooze the current task.
	 * 
	 * @param t
	 *            the task to snooze.
	 */
	protected void snoozeTask(FluidTask t)
	{
		String snoozeMessage = _Scheduler.snooze(t);
		if (snoozeMessage != null)
			Toast.makeText(getApplicationContext(), snoozeMessage,
					Toast.LENGTH_LONG).show();
		else
			finish();
	}

	/**
	 * Finish the popup activity.
	 */
	private void acceptTask()
	{
		finish();
	}

}